//
//  GoodsInfoViewController.m
//  HDYS
//
//  Created by qianfeng on 15-4-21.
//  Copyright (c) 2015年 kay. All rights reserved.
//

#import "GoodsInfoViewController.h"
#import "FirstTableViewCell.h"
#import "DescriptTableViewCell.h"
#import "UserJudgeTableViewCell.h"
#import "BuyOptionsView.h"
#import "UIImageView+WebCache.h"
#import "Header.h"
#import "PayViewController.h"


@interface GoodsInfoViewController () <UITableViewDataSource, UITableViewDelegate, BuyOptionsViewDelegate, UIScrollViewDelegate>

//保存选择尺寸按钮时的前一个按钮
@property (nonatomic, strong) UIButton *preSizeBtn;
//保存选择颜色按钮时的前一个按钮
@property (nonatomic, strong) UIButton *preColorBtn;

//@property (nonatomic, strong) UIScrollView *goodsSv;

//最下层的view
@property (nonatomic, strong) UIView *backView;

@property (nonatomic, strong) BuyOptionsView *optionView;

@property (nonatomic, strong) UIVisualEffectView *blurView;

@property (nonatomic, strong) UITableView *infoTableView;
//保存前一次的偏移量y
@property (nonatomic, assign) CGFloat preContentOffsetY;

@property (nonatomic, strong) UIImageView *goodsImg;

//检测img是否在拉伸
@property (nonatomic, assign) BOOL isStretch;

@end

@implementation GoodsInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationController.navigationBar setHidden:YES];
    self.view.backgroundColor = [UIColor blackColor];
    
    //拿到数据
//    NSLog(@"%@",self.model);
    
    [self createUI];
}

-(void)viewDidDisappear:(BOOL)animated{
    [self.navigationController.navigationBar setHidden:NO];
}


-(void)createUI{
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    backView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:backView];
    self.backView = backView;
    
    UIImageView *goodsImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [backView addSubview:goodsImg];
    self.goodsImg = goodsImg;
    NSString *urlStr = [URL_PRE stringByAppendingString:self.model.pic];
    [goodsImg setImageWithURL:[NSURL URLWithString:urlStr]];
    
    //表头
    UIButton *tableHeaderBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    tableHeaderBtn.frame = CGRectMake(0, 0, SCREEN_WIDTH, 340);
    tableHeaderBtn.backgroundColor = [UIColor clearColor];
    [tableHeaderBtn addTarget:self action:@selector(tableHeaderBtnClick) forControlEvents:UIControlEventTouchUpInside];
    //表尾
    UIButton *tableFooterView = [UIButton buttonWithType:UIButtonTypeCustom];
    tableFooterView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 20);
    [tableFooterView setTitle:@"查看更多评论" forState:UIControlStateNormal];
    [tableFooterView setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    tableFooterView.backgroundColor = [UIColor whiteColor];
    tableFooterView.titleLabel.font = [UIFont systemFontOfSize:14];
    tableFooterView.titleLabel.textAlignment = NSTextAlignmentCenter;
    
    //商品详情表视图
    UITableView *infoTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 40 + 20) style:UITableViewStyleGrouped];
    infoTableView.backgroundColor = [UIColor clearColor];
    infoTableView.tableHeaderView = tableHeaderBtn;
    infoTableView.tableFooterView = tableFooterView;
    
    infoTableView.tag = 301;
    infoTableView.sectionHeaderHeight = 10;
    infoTableView.sectionFooterHeight = 10;
    
    infoTableView.delegate = self;
    infoTableView.dataSource = self;
    [backView addSubview:infoTableView];
    self.infoTableView = infoTableView;
    
    //底部视图
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 40, SCREEN_WIDTH, 40)];
    bottomView.backgroundColor = [UIColor blackColor];
    [backView addSubview:bottomView];
    
    UIButton *likeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [likeBtn setTitle:@"加入收藏" forState:UIControlStateNormal];
    likeBtn.frame = CGRectMake(5, 5, (SCREEN_WIDTH - 20) / 3, 30);
    likeBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [likeBtn addTarget:self action:@selector(likeBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:likeBtn];
    
    UIButton *listCarBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [listCarBtn setTitle:@"加入购物车" forState:UIControlStateNormal];
    listCarBtn.frame = CGRectMake(likeBtn.frame.origin.x + likeBtn.frame.size.width + 5, likeBtn.frame.origin.y, likeBtn.frame.size.width, likeBtn.frame.size.height);
    listCarBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [listCarBtn addTarget:self action:@selector(listCarBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:listCarBtn];
    
    UIButton *buyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [buyBtn setTitle:@"立即购买" forState:UIControlStateNormal];
    buyBtn.frame = CGRectMake(listCarBtn.frame.origin.x + listCarBtn.frame.size.width + 5, listCarBtn.frame.origin.y, listCarBtn.frame.size.width, listCarBtn.frame.size.height);
    buyBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [buyBtn addTarget:self action:@selector(buyBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:buyBtn];
    
    CGFloat viewH = 405.0f;
    BuyOptionsView *optionView = [[BuyOptionsView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, viewH)];
    
    //购买的代理
    optionView.delegate = self;
    [optionView config:self.model];
    [self.view addSubview:optionView];
    self.optionView = optionView;
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(10, 30, 30, 20);
    [backBtn setTitle:@"返回" forState:UIControlStateNormal];
    backBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [backBtn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.backView addSubview:backBtn];
}

#pragma mark - 按钮点击
-(void)likeBtnClick{
    
}

-(void)listCarBtnClick{
    //加入购物车 提交到服务器
    
}

//立即购买
-(void)buyBtnClick{
    UIVisualEffectView *blurView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]];
    blurView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    blurView.alpha = 0;
    [self.view addSubview:blurView];
    self.blurView = blurView;
    
    
    
    [UIView animateWithDuration:0.5 animations:^{
        [self.view insertSubview:self.optionView aboveSubview:blurView];
        self.optionView.frame = CGRectMake(0, SCREEN_HEIGHT - 405, SCREEN_WIDTH, 405);
        
        blurView.alpha = 0.9;
        self.backView.transform = CGAffineTransformScale(self.view.transform, 0.8, 0.8);
    }];
}

#warning 表视图往上滚时返回
-(void)backBtnClick {
    self.goodsImg.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
//    NSLog(@"self.infoTableView.decelerating  = %d",self.infoTableView.decelerating); 

    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - BuyOptionsView协议方法
- (void)BuyOptionsViewCancle {
    [UIView animateWithDuration:0.5 animations:^{
        self.blurView.alpha = 0;
        self.optionView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 405);
        self.backView.transform = CGAffineTransformScale(self.view.transform, 1.0, 1.0);
    }];
    [self.blurView removeFromSuperview];
    self.blurView = nil;
}

//选中商品尺寸数量  跳到支付页面
- (void)BuyOptionsViewWillBuyGoodsWithSize:(NSString *)sizeStr count:(NSString *)count {
    PayViewController *payVC = [[PayViewController alloc] init];
    [self presentViewController:payVC animated:YES completion:nil];
}


#pragma mark - 表视图协议方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 10;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        static NSString *firstCellIdentifier = @"firstCellId";
        FirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:firstCellIdentifier];
        if (cell == nil) {
            cell = [[FirstTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:firstCellIdentifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        [cell configUIWithModel:self.model];
        return cell;
    }  else if (indexPath.section == 1){
        static NSString *ThirdCellIdentifier = @"ThirdCellId";
        DescriptTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ThirdCellIdentifier];
        if (cell == nil) {
            cell = [[DescriptTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ThirdCellIdentifier];
        }
        return cell;
    } else{
        static NSString *userJellIdentifier = @"userJudgeCellId";
        UserJudgeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:userJellIdentifier];
        if (cell == nil) {
            cell = [[UserJudgeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:userJellIdentifier];
        }
        return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 70.0f;
    } else if (indexPath.section == 1) {
        NSString *text = @"这件牛仔裤采用高档舒适的面料，款式新颖，更显档次，显瘦塑身，适合不同风格的美女，穿着季节长，百搭款，性价比高，绝对值得拥有。尺和厘米的转换:[1尺=33.33厘米，1厘米大概是0.03尺]";
        CGSize resize = [text sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:CGSizeMake(SCREEN_WIDTH - 20, 10000) lineBreakMode:NSLineBreakByWordWrapping];
        return 40.0f + resize.height;
    } else {
        NSString *text = @"这件牛仔裤采用高档舒适的面料，款式新颖，更显档次，显瘦塑身，适合不同风格的美女，穿着季节长，百搭款，性价比高，绝对值得拥有。尺和厘米的转换:[1尺=33.33厘米，1厘米大概是0.03尺]";
        CGSize resize = [text sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:CGSizeMake(SCREEN_WIDTH - 20, 10000) lineBreakMode:NSLineBreakByWordWrapping];
        return 65.0f + resize.height;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 5;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0.0000000000000001;
    }
    return 5;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20)];
//    if  (section == 2) {
//        footView.backgroundColor = [UIColor whiteColor];
//        return footView;
//    }
    footView.backgroundColor = [UIColor grayColor];
    return footView;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20)];
    headerView.backgroundColor = [UIColor grayColor];
    return headerView;
}

#pragma mark - scrollview协议方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.y > self.preContentOffsetY) { //往上滚
        if (self.isStretch == YES) {
            if (scrollView.contentOffset.y == 0) {
                self.isStretch = NO;
            } else {
                //缩小
                CGFloat changeScale = SCREEN_HEIGHT / (scrollView.contentOffset.y - self.preContentOffsetY + SCREEN_HEIGHT);
                self.goodsImg.transform = CGAffineTransformScale(self.goodsImg.transform, changeScale, changeScale);
                self.preContentOffsetY = scrollView.contentOffset.y;
            }
        } else {
            CGFloat changeLength = scrollView.contentOffset.y - self.preContentOffsetY;
            self.goodsImg.transform = CGAffineTransformTranslate(self.goodsImg.transform, 0, -changeLength);
            self.preContentOffsetY = scrollView.contentOffset.y;

        }
    } else { //往下滚 偏移量小于上一次
        if (scrollView.contentOffset.y > 0) { //本来在上 往下
            CGFloat changeLength = scrollView.contentOffset.y - self.preContentOffsetY;
            self.goodsImg.transform = CGAffineTransformTranslate(self.goodsImg.transform, 0, -changeLength);
            self.preContentOffsetY = scrollView.contentOffset.y;
        } else {
            CGFloat changeScale = (self.preContentOffsetY - scrollView.contentOffset.y + SCREEN_HEIGHT) / SCREEN_HEIGHT;
            self.goodsImg.transform = CGAffineTransformScale(self.goodsImg.transform, changeScale, changeScale);
            self.preContentOffsetY = scrollView.contentOffset.y;
            self.isStretch = YES;
        }
    }
}

#pragma mark - 浏览图片按钮点击
- (void)tableHeaderBtnClick {
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}



@end
